﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class BCompanyM : INTPage
{
    public int rowNo = 0;
    CommonMethod comHelp = new CommonMethod();
    CompanyDAO comm = new CompanyDAO();
    private CompanyModel model = new CompanyModel();

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        if (!IsPostBack)
        {
            comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "BCompany" }, gvCo, new string[] { "cmdAdd", "cmdEdit" }, new int[] { 4, 5 }, CommonModel.loginOut2);

             bindCo();
        }
    }
    //绑定公司
    public void bindCo()
    {
        gvCo.DataSource = comm.GetCompany();
        gvCo.DataBind();
    }
    // 换页
    protected void gvCo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvCo.PageIndex = e.NewPageIndex;
            bindCo();
        }
        catch
        {
        }
    }

    protected void gvCo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        rowNo++;
        //鼠标经过事件
        if (e.Row.RowIndex != -1)
        {
            //设置悬浮鼠标指针形状为"小手"
            e.Row.Attributes["style"] = "Cursor:hand";
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "lastColor=this.style.backgroundColor;this.style.backgroundColor='#CCCC99';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=lastColor;");
                if ("zh-cn".Equals(Session["Language"].ToString()))
                {
                    ((LinkButton)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0]).Attributes.Add("onclick", "return confirm('确实要删除此公司？');");
                }
                else
                {
                    ((LinkButton)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0]).Attributes.Add("onclick", "return confirm('OK to delete?');");
                }
            }
        }
    }
     // 添加
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl2);
        model.CoCode = txtCoCode.Text;
        model.CoName = txtCoName.Text;
        model.CoTel = txtTel.Text;
        model.CoFax = txtFax.Text;
        model.CoAddress = txtCoAddress.Text;
        model.CoDesc = txtDesc.Text;
        model.UserCode = Session["UserCode"].ToString();
        model.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm");               

        if (comm.AddCompany(model)>0)
        {
            CommonMethod.regAlertMessage("操作成功-Success!", this);
            bindCo();
            SetIfEdit(false);
        }
        else
        {
            CommonMethod.regAlertMessage("出现错误-Error!", this);
        }
    }
    // 编辑
    protected void cmdEdit_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl2);

        model.CoCode = txtCoCode.Text;
        model.CoName = txtCoName.Text;
        model.CoTel = txtTel.Text;
        model.CoFax = txtFax.Text;
        model.CoAddress = txtCoAddress.Text;
        model.CoDesc = txtDesc.Text;
        model.UserCode = Session["usercode"].ToString();
        model.date = DateTime.Now.ToString("yyyy-MM-dd hh:mm");

        if (comm.EditCompany(model) > 0)
        {
            CommonMethod.regAlertMessage("操作成功-Success!", this);
            bindCo();
            SetIfEdit(false);
        }
        else
        {
            CommonMethod.regAlertMessage("出现错误-Error!", this);
        }
    }
    // 删除
    protected void gvCo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string coCode = gvCo.Rows[e.RowIndex].Cells[1].Text;

        if (comm.DeleteCompany(coCode) > 0)
        {
            CommonMethod.regAlertMessage("操作成功-Success!", this);
            bindCo();
            SetIfEdit(false);
        }
        else
        {
            CommonMethod.regAlertMessage("无法删除，还在使用-Disable Delete", this);
        }
    }
    // 编辑绑定
    protected void gvCo_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtCoCode.Text = gvCo.Rows[gvCo.SelectedIndex].Cells[1].Text;
        txtCoName.Text = gvCo.Rows[gvCo.SelectedIndex].Cells[2].Text;
        txtTel.Text = gvCo.Rows[gvCo.SelectedIndex].Cells[3].Text;
        txtFax.Text = gvCo.Rows[gvCo.SelectedIndex].Cells[4].Text;
        txtCoAddress.Text = gvCo.Rows[gvCo.SelectedIndex].Cells[5].Text;
        txtDesc.Text = gvCo.Rows[gvCo.SelectedIndex].Cells[6].Text;
        SetIfEdit(true);
    }
    
    private void SetIfEdit(bool isEdit)
    {
        cmdAdd.Enabled = !isEdit;
        cmdEdit.Enabled = isEdit;
        txtCoCode.ReadOnly = isEdit;
        if (!isEdit)
        {
            txtTel.Text = string.Empty;
            txtFax.Text = string.Empty;
            txtDesc.Text = string.Empty;
            txtCoCode.Text = string.Empty;
            txtCoName.Text = string.Empty;
            txtCoAddress.Text = string.Empty;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SetIfEdit(false);
    }
}
